﻿using System;
using System.Web.UI.WebControls;
using WorkOrder.Business;
using System.Data;
using System.Collections;

namespace WorkOrder
{
    public partial class myWorkOrder : System.Web.UI.Page
    {
        Business_Table_Work_order b_t_w;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
                return;
            }
            b_t_w = new Business_Table_Work_order();
            if (!Page.IsPostBack)
            {
                
                ddlOrderType.Items.Clear();
                ddlOrderType.DataTextField = "order_type";
                ddlOrderType.DataValueField = "order_type_id";
                ddlOrderType.DataSource = b_t_w.GetOrderType();
                ddlOrderType.DataBind();
                ddlOrderType.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlOrderType.SelectedIndex = ddlOrderType.Items.Count - 1;

                ddlAccessMode.Items.Clear();
                ddlAccessMode.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlAccessMode.Items.Add(new ListItem("传统方式接入", "传统方式接入"));
                ddlAccessMode.Items.Add(new ListItem("GPON接入", "GPON接入"));
                ddlAccessMode.Items.Add(new ListItem("无线接入", "无线接入"));
                ddlAccessMode.SelectedIndex = 0;

                ddlStatus.Items.Clear();
                ddlStatus.DataTextField = "order_status";
                ddlStatus.DataValueField = "order_status";
                ddlStatus.DataSource = b_t_w.GetOrderStatus();
                ddlStatus.DataBind();
                ddlStatus.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlStatus.SelectedIndex = ddlStatus.Items.Count - 1;
                

                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = b_t_w.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;

                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = b_t_w.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;

                seleArea.Items.Clear();
                seleArea.DataTextField = "area_name";
                seleArea.DataValueField = "area_id";
                seleArea.DataSource = b_t_w.GetArea();
                seleArea.DataBind();
                seleArea.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleArea.SelectedIndex = seleArea.Items.Count - 1;
                
                GridView1.Attributes.Add("SortExpression", "task_time");
                GridView1.Attributes.Add("SortDirection", "DESC");
                txtRow.Text = GridView1.PageSize.ToString();

                string tmp = DateTime.Now.Year.ToString();
                if (DateTime.Now.Month < 10) tmp += "-0" + DateTime.Now.Month + "-01"; else tmp += "-" + DateTime.Now.Month + "-01";
                txtSTART_DATE.Value = tmp;
                tmp = DateTime.Now.Year.ToString();
                if (DateTime.Now.Month < 10) tmp += "-0" + DateTime.Now.Month; else tmp += "-" + DateTime.Now.Month;
                if (DateTime.Now.Day < 10) tmp += "-0" + DateTime.Now.Day; else tmp += "-" + DateTime.Now.Day;
                txtEND_DATE.Value = tmp;

                BindGridView();
            }
        }
        #region 查询数据
        private void BindGridView() 
        {
            string sqlstr = "1=1 and entry_person=" + Session["UserID"].ToString();
            if (!string.IsNullOrEmpty(txtOrderNo.Value.Trim()))
                sqlstr += " and order_number='" + Server.HtmlEncode(txtOrderNo.Value.Trim()) + "'";
            if (!string.IsNullOrEmpty(txtTitle.Text.Trim()))
                sqlstr += " and title like '%" + Server.HtmlEncode(txtTitle.Text.Trim()) + "%'";
            if (ddlProduct.SelectedValue != "0")
                sqlstr += " and product_id=" + ddlProduct.SelectedValue;
            if (ddlOrderType.SelectedValue != "0")
                sqlstr += " and order_type_id=" + ddlOrderType.SelectedValue;
            if (seleTown.Value != "0")
                sqlstr += " and town_id=" + seleTown.Value;
            else if (seleArea.Value != "0")
            {
                ArrayList arr = b_t_w.GetTownIdByArea(seleArea.Value);
                string tmp = "";
                if (arr != null && arr.Count > 0)
                {
                    for (int i = 0; i < arr.Count; i++)
                        tmp += " or town_id=" + arr[i].ToString();
                }

                sqlstr += " and (" + tmp.Substring(4, tmp.Length - 4) + ")";
            }
            if (ddlStatus.SelectedValue != "0")
                sqlstr += " and order_status='" + ddlStatus.SelectedValue + "'";
            if (ddlAccessMode.SelectedValue != "0")
                sqlstr += " and access_mode='" + ddlAccessMode.SelectedValue + "'";
            
            if (rbYes.Checked)
                sqlstr += " and finish_time is not null and (order_status='已关闭' or order_status='待业务确认') and is_back=0";
            if (rbNo.Checked)
                sqlstr += " and finish_time is null and finish_time<='2000-1-1'";

            if (txtSTART_DATE.Value != "" && txtEND_DATE.Value != "")
                sqlstr += " and task_time between '" + txtSTART_DATE.Value + " 0:0:0' and '" + txtEND_DATE.Value + " 23:59:59'";

            DataTable dt = b_t_w.QueryDatas(sqlstr);

            if (dt != null)
                hidRowCount.Value = dt.Rows.Count.ToString();
            lblRowCount.Text = "共&nbsp;" + hidRowCount.Value + "&nbsp;条记录";

            try { GridView1.PageSize = Convert.ToInt32(txtRow.Text.Trim()); }
            catch { }

            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                if (dt != null)
                    dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }

            // GridView绑定并显示数据
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        #endregion

        #region 处理光标行颜色事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
        #endregion

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                GridView1.PageIndex = e.NewPageIndex;
                BindGridView();
                TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum"); tb.Text = (GridView1.PageIndex + 1).ToString();
            }
            catch { }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "go")
            {
                try
                {
                    TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    GridView1_PageIndexChanging(null, ea);
                }
                catch
                { }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView1.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }

            // 重新设定GridView排序数据列及排序方向
            GridView1.Attributes["SortExpression"] = sortExpression;
            GridView1.Attributes["SortDirection"] = sortDirection;

            BindGridView();
        }

        #region 查询按钮事件
        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            BindGridView();
        }
        #endregion 
    }
}
